package javax.core.common.encrypt;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * DES 静态key加密解密
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/8/31 21:12
 */
public class DESStaticKey {

    /**
     * DES 加密，解决中文乱码问题
     *
     * @param str 需要加密的字符串
     * @param key 密钥
     * @param charset 编码
     * @return
     */
    @SuppressWarnings("all")
    public static String encrypt(String str, String key, String charset) {
        try {
            if (str == null || str.length() < 1) {
                return "";
            }
            DESKeySpec keySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            Cipher c1 = Cipher.getInstance("DES");
            c1.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] cipherByte = c1.doFinal(str.getBytes());
            String result = bytes2Hex(cipherByte);
            if (!(null == charset || charset.length() < 1)) {
                result = URLEncoder.encode(result, charset);
            }
            return result;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * DES 加密，解决中文乱码问题
     *
     * @param str 需解密的字符串
     * @param key 密钥
     * @param charset 编码
     * @return
     */
    @SuppressWarnings("all")
    public static String decrypt(String str, String key, String charset) {
        try {
            if (str == null || str.length() < 1) {
                return "";
            }
            DESKeySpec keySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            Cipher c1 = Cipher.getInstance("DES");
            c1.init(Cipher.DECRYPT_MODE, secretKey);
            String result = new String(c1.doFinal(hex2byte(str)));
            if (!(null == charset || charset.length() < 1)) {
                result = URLDecoder.decode(result, charset);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 加密
     */
    public static String encrypt(String str, String key) {
        return encrypt(str, key, null);
    }

    /**
     * 解密
     */
    public static String decrypt(String str, String key) {
        return decrypt(str, key, null);
    }

    private static String bytes2Hex(byte[] bts) {
        StringBuilder builder = new StringBuilder();
        String tmp = null;
        for (byte bt: bts) {
            tmp = (Integer.toHexString(bt & 0xFF));
            if (tmp.length() == 1) {
                builder.append("0");
            }
            builder.append(tmp);
        }
        return builder.toString();
    }

    private static byte[] hex2byte(String hexStr) {
        try {
            byte[] bts = new byte[hexStr.length() / 2];
            for (int i = 0, j = 0; j < bts.length; j++) {
                bts[j] = (byte) Integer.parseInt(hexStr.substring(i, i + 2), 16);
                i += 2;
            }
            return bts;
        } catch (Exception e) {
            return "".getBytes();
        }
    }

    public static void main(String[] args) throws Exception {
        String str = "6cb2606b4382afd1659a4b4031f65a2c6ef727f10c03de27f4ed22e84d8ef49a8ca9af6dd4f68fc2a9c1f322be1043efd50b4070bb33a84c1c4433fdfea98cb913f7f76703e7c5ded1c494c2cc288c4bd055d335bf2a0a779135eb694b5da73e6499b1cd74d2b3e957eb793aabc23b1ca7949c3eb28abfce76ce7c7e4120d8ecbd74b61603637e817a7fce656a32f81ecea8a1ac96c701c6";
        System.out.println(DESStaticKey.decrypt(str, "402880E6"));
    }

}
